package com.yoda.mall.mysql.api;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yoda.common.exception.BusinessException;
import com.yoda.common.exception.ErrorResult;
import com.yoda.mall.api.ResourceApi;
import com.yoda.mall.api.ResourceCategoryApi;
import com.yoda.mall.model.entity.ums.Resource;
import com.yoda.mall.model.entity.ums.ResourceCategory;
import com.yoda.mall.mysql.mapper.ResourceCategoryMapper;
import com.yoda.mall.mysql.mapper.ResourceMapper;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@DubboService
public class ResourceCategoryApiImpl implements ResourceCategoryApi {
    @Autowired
    private ResourceCategoryMapper resourceCategoryMapper;
    @Autowired
    private ResourceApi resourceApi;

    @Override
    public List<ResourceCategory> list() {
        return resourceCategoryMapper.selectList(null);
    }

    @Override
    public void create(ResourceCategory resourceCategory) {
        resourceCategoryMapper.insert(resourceCategory);
    }

    @Override
    public void deleteById(Integer id) {
        List<Resource> resources = resourceApi.findByCategoryId(id);
        if(CollUtil.isNotEmpty(resources)){
            throw new BusinessException(ErrorResult.resourceCategoryError());
        }
        resourceCategoryMapper.deleteById(id);
    }

    @Override
    public void updateById(ResourceCategory resourceCategory) {
        resourceCategoryMapper.updateById(resourceCategory);
    }
}
